//
//  ExchangeViewController.m
//  LineMoney
//
//  Created by 范琦 on 16/7/8.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "ExchangeViewController.h"
#import "ExchangeTopView.h"
#import "ExchangeTwoTopView.h"
#import "ExchangeRateView.h"
#import "ExchangeMainView.h"
#import "ExchangeMainView2.h"
#import "ExchangeTwoViewViewController.h"
#import "ExchangeJS.h"

@interface ExchangeViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) ExchangeTopView *topView;
@property (nonatomic, strong) ExchangeTwoTopView *twoTopView;
@property (nonatomic, strong) ExchangeTwoTopView *twoTopView2;

@property (nonatomic, strong) ExchangeRateView *rateView;
@property (nonatomic, strong) ExchangeRateView *rateView2;



@property (nonatomic, strong) UIScrollView *mainScroll;

@property (nonatomic, strong) UIScrollView *mainView2;

@property (nonatomic, assign) BOOL isCity;
@property (nonatomic, assign) BOOL isCity2;

@property (nonatomic, strong) UIScrollView *mainView;


@property (nonatomic, strong) UIPickerView *pickView;

@property (nonatomic, strong) UITextField *textFiled; // 当前选中的text

@property (nonatomic, strong) NSMutableArray *listArr;

@property (nonatomic, strong) ExchangeJS *exchangeJs;

@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) NSMutableArray *yearArr;
@property (nonatomic, strong) NSMutableArray *monthArr;
@property (nonatomic, strong) NSMutableArray *dayArr;
@property (nonatomic, assign) BOOL isYear;

@property (nonatomic, strong) NSString *yearStr;
@property (nonatomic, strong) NSString *monthStr;
@property (nonatomic, strong) NSString *dayStr;
@property (nonatomic, strong) NSString *timeStr;

@end

@implementation ExchangeViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"兑换版";
    self.listArr = [NSMutableArray arrayWithObjects:@"CNY",@"USD",@"法国", nil];
    self.yearArr = [NSMutableArray arrayWithObjects:@"2014",@"2015",@"2016", nil];
    self.monthArr = [NSMutableArray arrayWithObjects:@"3",@"4",@"5", nil];
    self.dayArr = [NSMutableArray arrayWithObjects:@"15",@"16",@"17", nil];
    self.isYear = NO;
    self.yearStr = @"2016";
    self.monthStr = @"3";
    self.dayStr = @"21";
    
    [self creatScroll];
    [self creatPickerView];
    [self creatTopView];
    [self creatTwoTopView];
    [self creatRateView];
    [self creatBottomMainView];
    
    
    
}
#warning 创建大Scroll
- (void)creatScroll
{
    self.mainScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 35, KSCREENWIDTH, KSCREENHEIGHT)];
    _mainScroll.contentSize = CGSizeMake(KSCREENWIDTH * 2, 0);
    [self.view addSubview:_mainScroll];
    _mainScroll.backgroundColor = HUIBACKGROUNDCOLOR;
    _mainScroll.pagingEnabled = YES;
    
    [RACObserve(_mainScroll, contentOffset) subscribeNext:^(id x) {
        NSValue *toValue = (NSValue *)x;
        CGPoint c;
        [toValue getValue:&c];
        NSInteger i = c.x / KSCREENWIDTH;
        if (i == 0) {
            _topView.leftBtn.tintColor = BACKGROUNDCOLOR;
            _topView.rightBtn.tintColor = TEXTCOLOR;
        }else{
            _topView.leftBtn.tintColor = TEXTCOLOR;
            _topView.rightBtn.tintColor = BACKGROUNDCOLOR;
        }
        
    }];


    self.mainView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    _mainView.contentSize = CGSizeMake(0, 0);
    [_mainScroll addSubview:_mainView];
    _mainView.backgroundColor = HUIBACKGROUNDCOLOR;
    _isCity = YES;
    _isCity2 = YES;
    
    
    self.mainView2 = [[UIScrollView alloc]initWithFrame:CGRectMake(KSCREENWIDTH, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    _mainView2.contentSize = CGSizeMake(0, 730);
    [_mainScroll addSubview:_mainView2];
    _mainView2.backgroundColor = HUIBACKGROUNDCOLOR;
    _mainView2.showsVerticalScrollIndicator = NO;
    
    
}
#warning 创建最上方view
- (void)creatTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeTopView" owner:nil options:nil] lastObject];
    _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, 99);
    [self.view addSubview:_topView];
    _topView.leftBtn.tintColor = BACKGROUNDCOLOR;
    _topView.leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _topView.rightBtn.tintColor = TEXTCOLOR;
    _topView.rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _topView.lineView.backgroundColor = HUIBACKGROUNDCOLOR;
    [[_topView.leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        _topView.rightBtn.tintColor = TEXTCOLOR;
        _topView.leftBtn.tintColor = BACKGROUNDCOLOR;
        [_mainScroll setContentOffset:CGPointMake(0, 0) animated:YES];
    }];
    [[_topView.rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        _topView.rightBtn.tintColor = BACKGROUNDCOLOR;
        _topView.leftBtn.tintColor = TEXTCOLOR;
        [_mainScroll setContentOffset:CGPointMake(KSCREENWIDTH, 0) animated:YES];

    }];
}
#warning 创建上方四步图片
- (void)creatTwoTopView
{
    self.twoTopView = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeTwoTopView" owner:nil options:nil] lastObject];
    _twoTopView.frame = CGRectMake(0, 0, KSCREENWIDTH, 75);
    [self.mainView addSubview:_twoTopView];
    
    self.twoTopView2 = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeTwoTopView" owner:nil options:nil] lastObject];
    _twoTopView2.frame = CGRectMake(0, 0, KSCREENWIDTH, 75);
    [self.mainView2 addSubview:_twoTopView2];
}
#warning 创建今日汇率
- (void)creatRateView
{
    self.rateView = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeRateView" owner:nil options:nil] lastObject];
    _rateView.frame = CGRectMake(0, 85, KSCREENWIDTH, 50);
    [self.mainView addSubview:_rateView];
 
    _rateView.leftLabel.textColor = TEXTCOLOR;
    _rateView.numLabel.textColor = BACKGROUNDCOLOR;
    
    
    self.rateView2 = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeRateView" owner:nil options:nil] lastObject];
    _rateView2.frame = CGRectMake(0, 85, KSCREENWIDTH, 50);
    [self.mainView2 addSubview:_rateView2];
    
    _rateView2.leftLabel.textColor = TEXTCOLOR;
    _rateView2.numLabel.textColor = BACKGROUNDCOLOR;

}
#warning 创建主体View
- (void)creatBottomMainView
{
    self.bottomMainView = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeMainView" owner:nil options:nil] lastObject];
    _bottomMainView.frame = CGRectMake(0, 145, KSCREENWIDTH, 335);
    [self.mainView addSubview:_bottomMainView];
    [[_bottomMainView.leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self creatExchangeJs];
    }];
    
    [[_bottomMainView.useCityBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (_isCity == YES) {
            _bottomMainView.useCityImg.image = [UIImage imageNamed:@"选中"];
            _bottomMainView.wantExchange.userInteractionEnabled = NO;
            _bottomMainView.wantExchange.backgroundColor = HUIBACKGROUNDCOLOR;
            _isCity = NO;
        }else{
            _bottomMainView.useCityImg.image = [UIImage imageNamed:@"未选中"];
            _bottomMainView.wantExchange.userInteractionEnabled = YES;
            _bottomMainView.wantExchange.backgroundColor = [UIColor whiteColor];
            _isCity = YES;
        }
    }];
    
    _bottomMainView.exchangeStyleText.inputView = _pickView;
    _bottomMainView.exchangeToText.inputView = _pickView;
    
    [[_bottomMainView.exchangeStyleText rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
        if (_bottomMainView.exchangeStyleText.text.length == 0) {
            _bottomMainView.exchangeStyleText.text = @"CNY";
        }
        self.textFiled = x;
    }];

    [[_bottomMainView.exchangeToText rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
        if (_bottomMainView.exchangeToText.text.length == 0) {
            _bottomMainView.exchangeToText.text = @"CNY";
        }
        self.textFiled = x;
    }];
    
    [[_bottomMainView.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        ExchangeTwoViewViewController *C = [[ExchangeTwoViewViewController alloc]init];
        [self.navigationController pushViewController:C animated:YES];
    }];
    
    
    
    self.bottomMainView2 = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeMainView2" owner:nil options:nil] lastObject];
    _bottomMainView2.frame = CGRectMake(0, 145, KSCREENWIDTH, 430);
    [self.mainView2 addSubview:_bottomMainView2];
    
    _bottomMainView2.replyType.inputView = _pickView;
    _bottomMainView2.startBeginText.inputView = _pickView;
    
    [[_bottomMainView2.replyType rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
        self.textFiled = x;
        [self closePickerView];
    }];
    
    [[_bottomMainView2.startBeginText rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
        self.textFiled = x;
        self.isYear = YES;
        [self closePickerView];
    }];
    [[_bottomMainView2.startBeginText rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x) {
        self.textFiled = x;
        self.isYear = NO;
        
    }];
    [[_bottomMainView2.leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self creatExchangeJs];
    }];
    
    [[_bottomMainView2.useCityBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (_isCity2 == YES) {
            _bottomMainView2.useCityImg.image = [UIImage imageNamed:@"选中"];
            _bottomMainView2.wantExchange.userInteractionEnabled = NO;
            _bottomMainView2.wantExchange.backgroundColor = HUIBACKGROUNDCOLOR;
            _isCity2 = NO;
        }else{
            _bottomMainView2.useCityImg.image = [UIImage imageNamed:@"未选中"];
            _bottomMainView2.wantExchange.userInteractionEnabled = YES;
            _bottomMainView2.wantExchange.backgroundColor = [UIColor whiteColor];
            _isCity2 = YES;
        }
    }];
    
    [[_bottomMainView2.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        ExchangeTwoViewViewController *C = [[ExchangeTwoViewViewController alloc]init];
        [self.navigationController pushViewController:C animated:YES];
    }];
    
    _bottomMainView2.exchangeStyleText.inputView = _pickView;
    _bottomMainView2.exchangeToText.inputView = _pickView;
    [[_bottomMainView2.exchangeStyleText rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
        [self closePickerView];
        if (_bottomMainView2.exchangeStyleText.text.length == 0) {
            _bottomMainView2.exchangeStyleText.text = @"CNY";
        }
        self.textFiled = x;
    }];
    
    [[_bottomMainView2.exchangeToText rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
        [self closePickerView];
        if (_bottomMainView2.exchangeToText.text.length == 0) {
            _bottomMainView2.exchangeToText.text = @"CNY";
        }
        self.textFiled = x;
    }];
    
    
    if (_exchangeStyleStr.length != 0) {
        _bottomMainView.exchangeStyleText.text = _exchangeStyleStr;
        _bottomMainView.exchangeToText.text = _exchangeToStr;
        _bottomMainView.exchangeMoneyText.text = _exchangeMoneyStr;
        _bottomMainView.wantExchange.text = _exchangeHLStr;
        
        _bottomMainView2.exchangeStyleText.text = _exchangeStyleStr;
        _bottomMainView2.exchangeToText.text = _exchangeToStr;
        _bottomMainView2.exchangeMoneyText.text = _exchangeMoneyStr;
        _bottomMainView2.wantExchange.text = _exchangeHLStr;
    }

}

#warning 当一个text开始编辑 先关闭所有的pickerView
- (void)closePickerView
{
//    if (_isYear == NO) {
        [self.pickView reloadAllComponents];
//    }
}
#warning 创建如何节省
- (void)creatExchangeJs
{
    
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 64)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.4;
    [self.view addSubview:_backView];
    
    
    _exchangeJs = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeJS" owner:nil options:nil] lastObject];
    [[_exchangeJs.closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_exchangeJs removeFromSuperview];
        [_backView removeFromSuperview];
    }];
    _exchangeJs.frame = CGRectMake(41, KSCREENHEIGHT, KSCREENWIDTH - 82, 420);
    [self.view addSubview:_exchangeJs];

    [UIView animateWithDuration:0.8 animations:^{
        _exchangeJs.frame = CGRectMake(41, 90 - 64, KSCREENWIDTH - 82, 420);

    }];
    _exchangeJs.backgroundColor = [UIColor clearColor];
}
#warning 创建PickerView
- (void)creatPickerView
{
    self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KSCREENHEIGHT - 150, KSCREENWIDTH, 150)];
    _pickView.delegate = self;
    _pickView.dataSource = self;
}
#warning PickerView代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (self.isYear == YES) {
        return 3;
    }
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (_isYear == YES) {
        if (component == 0) {
            return _yearArr[row];
        }
        if (component == 1) {
            return _monthArr[row];
        }
        return _dayArr[row];
    }
    return _listArr[row];
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (_isYear == YES) {
        
        if (component == 0) {
            _yearStr = _yearArr[row];
        }else if (component == 1){
            _monthStr = _monthArr[row];
        }else if (component == 2){
            _dayStr = _dayArr[row];
        }
        _timeStr = [NSString stringWithFormat:@"%@-%@-%@", _yearStr, _monthStr, _dayStr];
        self.textFiled.text = _timeStr;
    }else{
        self.textFiled.text = _listArr[row];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
